package org.brownhouse.rss;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;

/**
 * This is the main class to execute this program.
 */
public class Main {

    public static void main(String[] args) throws IOException {
        if (args == null || args.length != 2 || !new File(args[1]).isDirectory()) {
            System.out.println("USAGE: Main <input.properties> <destination dir>");
            return;
        }

        String propString = FileUtils.readFileToString(new File(args[0]));
        String[] props = propString.split("\n");
        String feedUrl = props[0];

        Set<String> regularExpressions = new HashSet<String>();
        for (int i = 1; i < props.length; i++) {
            regularExpressions.add(props[i]);
        }

        TorrentReader torrentReader = new RssTorrentReader(new URL(feedUrl), regularExpressions);

        TorrentDownloader torrentDownloader =
                new UniqueTorrentDownloader(torrentReader.getTorrents(), new File(args[1]));
        torrentDownloader.downloadTorrents();
    }
}
